package com.usian.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.usian.mapper.TbItemCatMapper;
import com.usian.mapper.TbItemMapper;
import com.usian.pojo.TbItem;
import com.usian.pojo.TbItemCat;
import com.usian.pojo.TbItemCatExample;
import com.usian.pojo.TbItemExample;
import com.usian.redis.RedisClient;
import com.usian.utils.CateGroup;
import com.usian.utils.FengZ;
import com.usian.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

@Service
public class ItemCateServiceImpl implements ItemCateService {

    @Autowired
    private TbItemCatMapper tbItemCatMapper;
    @Autowired
    private RedisClient redisClient;




    @Override
    public List<TbItemCat> selectItemCategoryByParentId(Integer id) {
        TbItemCatExample tbItemCatExample=new TbItemCatExample();
        TbItemCatExample.Criteria criteria = tbItemCatExample.createCriteria();
        criteria.andParentIdEqualTo((long)id);
        criteria.andStatusEqualTo(1);
        List<TbItemCat> list = tbItemCatMapper.selectByExample(tbItemCatExample);

        return list;
    }

    @Override
    public FengZ selectItemCategoryAll() {
        FengZ feng =(FengZ) redisClient.get("feng");
        if (feng!=null){
            return feng;
        }
        FengZ f=new FengZ();
        f.setData(getlist(0));
        redisClient.set("feng",f);
        return f;
    }
    public List getlist( long id){
        TbItemCatExample t=new TbItemCatExample();
        TbItemCatExample.Criteria criteria = t.createCriteria();
        criteria.andParentIdEqualTo(id);
        List<TbItemCat> list = tbItemCatMapper.selectByExample(t);
        List list1=new ArrayList<>();
        int count=0;
        for (TbItemCat tbItemCat : list) {
            if (tbItemCat.getIsParent()){
                CateGroup c=new CateGroup();
                c.setN(tbItemCat.getName());
                c.setI(getlist(tbItemCat.getId()));
                list1.add(c);
                count++;
                if (count==18){
                    break;
                }
            }else {
                list1.add(tbItemCat.getName());
            }
        }
        return list1;


    }
}
